package com.itita.ww2.model.item.prop;

import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.enums.WW2ExceptionCode;
import com.itita.ww2.model.item.enums.param1.P1ExpType;
import com.itita.ww2.model.item.interfaces.IUseable;
import com.itita.ww2.model.user.UserProfile;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class ExpPropStrategy implements IUseable {
	// private static final Logger logger = LoggerFactory.getLogger(ExpPropStrategy.class);
	private ItemProp itemProp;

	public ExpPropStrategy(ItemProp itemProp) {
		this.itemProp = itemProp;
	}

	@Override
	public ISFSObject use(UserProfile userProfile, ISFSObject params) throws WW2Exception {
		ISFSObject ref = SFSObject.newInstance();
		P1ExpType expPropType = P1ExpType.values()[itemProp.getParam1()];
		int addValue = (int) itemProp.getEffValue();
		switch (expPropType) {
		case ARMY:
			{
				String armyCode = params.getUtfString("armyCode");
				if (!userProfile.getArmyManager().isArmyUnlocked(armyCode)) {
					// 兵没解锁,不能加经验
					throw new WW2Exception(WW2ExceptionCode.USER_ARMY_ERROR);
				}
				boolean ret = userProfile.getArmyManager().getArmy(armyCode).growExp(addValue);
				if (ret) {
					userProfile.getArmyManager().update();
					userProfile.getArmyManager().syncArmy();
				} else {
					throw new WW2Exception(WW2ExceptionCode.SERVER_ERROR, "UseExpItem - [0x002]");
				}
			}
			break;
		case COMMANDER:
			{
				boolean ret = userProfile.getUserInfo().expGrow(addValue);
				if (ret) {
					userProfile.getUserInfo().update();
					userProfile.getUserInfo().syncExp();
				} else {
					throw new WW2Exception(WW2ExceptionCode.SERVER_ERROR, "UseExpItem - [0x003]");
				}
			}
			break;
		default:
			throw new WW2Exception();
		}
		return ref;
	}

}
